import { Channel } from '@/store/reducer/channel';
import { Auth } from '@/store/reducer/user';

// 将token存储在本地
export const setToken = (token: Auth): void => {
  return localStorage.setItem('token-geek-park', JSON.stringify(token));
};

// 获取token
export const getToken = (): Auth => {
  return JSON.parse(localStorage.getItem('token-geek-park') || '{}');
};

// 删除本地缓存中的token信息
export const removeToken = (): void => {
  return localStorage.removeItem('token-geek-park');
};

// 判断本地缓存中是否存在token
export const hasToken = (): boolean => {
  return Boolean(getToken().token);
};

// 将频道数据缓存到本地
export const setChannel = (channels: Channel[]) => {
  localStorage.setItem('channel-list', JSON.stringify(channels));
};

// 获取本地频道数据
export const getChannel = () => {
  return JSON.parse(localStorage.getItem('channel-list') || '[]');
};

// 清空频道数据
export const removeChannels = () => {
  localStorage.removeItem('channel-list');
};

// 将搜索历史关键字存入本地缓存
export const setHistory = (histories: string[]) => {
  localStorage.setItem('history', JSON.stringify(histories));
};

// 从缓存中获取搜索历史关键字
export const getHistory = () => {
  return JSON.parse(localStorage.getItem('history') || '[]');
};

// 删除本地缓存中的关键字
export const removeHistory = () => {
  localStorage.removeItem('history');
};
